/**
 * @author:albert
 * @date:2016年7月7日
 */
package com.bkbw.live.battle.bean;

import java.util.LinkedList;
import java.util.List;

import com.bkbw.live.battle.business.items.UnitBagItem;
import com.bkbw.live.battle.business.items.UnitBagItemChange;
import com.bkbw.live.battle.business.items.UnitEquipment;
import com.bkbw.live.battle.business.items.UnitWeapon;
import com.bkbw.live.battle.business.items.UnitBagItemChange.UnitBagItemOpType;
import com.bkbw.live.battle.database.bean.BagBeanManager;
import com.bkbw.live.battle.database.bean.BagBeanManager.BagItemBean;
import com.bkbw.live.common.business.ItemTypeUtil;
import com.bkbw.live.common.dataproto.BattleToDatabasePb.DbBagEquipment;
import com.bkbw.live.common.dataproto.BattleToDatabasePb.DbBagItem;
import com.bkbw.live.common.dataproto.BattleToDatabasePb.DbBagWeapon;
import com.bkbw.live.common.dataproto.ClientToBattleServerPb.BagEquipmentBase;
import com.bkbw.live.common.dataproto.ClientToBattleServerPb.BagItem;
import com.bkbw.live.common.dataproto.ClientToBattleServerPb.BagWeapon;
import com.bkbw.live.common.dataproto.CommonMessagePb.BagItemChangePb;
import com.bkbw.live.common.dataproto.CommonMessagePb.DBagItemPb;
import com.bkbw.live.common.dataproto.CommonMessagePb.DBagItemPbOpTypePb;
import com.bkbw.live.common.dataproto.CommonMessagePb.NatureAttr;
import com.bkbw.live.common.util.Utils;

/**
 * @author albert
 *
 */
public class BeanConvertor {
	// -------------- 把角色的背包数据转为游戏中使用的背包数据 --------------------
	// 把角色物品表的数据转为游戏中的背包数据
	public static UnitBagItem transDbBagItem2UnitBagItem(DbBagItem dbBagItem) {
		//if (dbBagItem.geti)
		BagItemBean bagItemBean = BagBeanManager.getInstance().getBagItemBeanById(dbBagItem.getItemId());
		if (ItemTypeUtil.isItem(bagItemBean.getItemType()) || ItemTypeUtil.isCollection(bagItemBean.getItemType())) {
			UnitBagItem unitBagItem = new UnitBagItem();
			Utils.copySuperFieldsFromBean(unitBagItem, dbBagItem);
			return unitBagItem;
		}
		return null;
	}
	// 把角色装备表的数据转为游戏中的背包数据
	public static UnitEquipment transDbBagEquipment2UnitEquipment(DbBagEquipment dbBagEquipment) {
		//if (dbBagItem.geti)
		BagItemBean bagItemBean = BagBeanManager.getInstance().getBagItemBeanById(dbBagEquipment.getItemId());
		if (ItemTypeUtil.isEquipment(bagItemBean.getItemType())) {
			UnitEquipment unitEquipment = new UnitEquipment();
			Utils.copySuperFieldsFromBean(unitEquipment, dbBagEquipment);
			return unitEquipment;
		}
		return null;
	}
	// 把角色武器表的数据转为游戏中的背包数据
	public static UnitWeapon transDbBagWeapon2UnitWeapon(DbBagWeapon dbBagWeapon) {
		//if (dbBagItem.geti)
		BagItemBean bagItemBean = BagBeanManager.getInstance().getBagItemBeanById(dbBagWeapon.getItemId());
		if (ItemTypeUtil.isWeapon(bagItemBean.getItemType())) {
			UnitWeapon unitWeapon = new UnitWeapon();
			Utils.copySuperFieldsFromBean(unitWeapon, dbBagWeapon);
			return unitWeapon;
		}
		return null;
	}
	
	// -------------- 把游戏中使用的背包数据转为客户端需要的BagItem --------------------
	// TODO 可以调用copyxx函数，减少代码量，但因为BagItem包含了其他结构，还是有一些手动操作需要处理
	// 把游戏中的背包数据转为客户端需要的BagItem
	public static BagItem doTransUnitBagItem2BagItem(UnitBagItem unitBagItem) {
		if (unitBagItem instanceof UnitWeapon) {
			return transUnitWeapon2BagItem((UnitWeapon)unitBagItem);
		}
		if (unitBagItem instanceof UnitEquipment) {
			return transUnitEquipment2BagItem((UnitEquipment)unitBagItem);
		}
		if (unitBagItem instanceof UnitWeapon) {
			return transUnitWeapon2BagItem((UnitWeapon)unitBagItem);
		}
		return null;
	}
	
	public static BagItem transUnitBagItem2BagItem(UnitBagItem unitBagItem) {
		BagItem.Builder bagItemBuilder = BagItem.newBuilder();
		bagItemBuilder.setItemId(unitBagItem.getItemId());
		bagItemBuilder.setUid(unitBagItem.getUid());
		bagItemBuilder.setNumber(unitBagItem.getNumber());
		bagItemBuilder.setParam(unitBagItem.getParam());
		return bagItemBuilder.build();
	}
	
	public static BagItem transUnitEquipment2BagItem(UnitEquipment unitEquipment) {
		BagItem.Builder bagItemBuilder = BagItem.newBuilder();
		bagItemBuilder.setItemId(unitEquipment.getItemId());
		bagItemBuilder.setUid(unitEquipment.getUid());
		bagItemBuilder.setNumber(unitEquipment.getNumber());
		// build equip extension of item
		BagEquipmentBase.Builder bagEquipmentBaseBuilder = BagEquipmentBase.newBuilder();
		NatureAttr.Builder natureAttrBuilder = NatureAttr.newBuilder();
		natureAttrBuilder.setMaxHp(unitEquipment.getMaxHp());
		//natureAttrBuilder.setDefence(unitEquipment.getArmor());
		natureAttrBuilder.setEvade(unitEquipment.getEvade());
		bagEquipmentBaseBuilder.setNature(natureAttrBuilder.build());
		bagEquipmentBaseBuilder.setSight(unitEquipment.getSight());
		bagEquipmentBaseBuilder.setEndurance(unitEquipment.getEndurance());
		bagEquipmentBaseBuilder.setMaxEndurance(unitEquipment.getMaxEndurance());
		bagEquipmentBaseBuilder.setIsEquip(unitEquipment.getIsEquip()==0?false:true);
		bagItemBuilder.setBagEquipmentBase(bagEquipmentBaseBuilder.build());
		return bagItemBuilder.build();
	}
	
	public static BagItem transUnitWeapon2BagItem(UnitWeapon unitWeapon) {
		BagItem.Builder bagItemBuilder = BagItem.newBuilder();
		bagItemBuilder.setItemId(unitWeapon.getItemId());
		bagItemBuilder.setUid(unitWeapon.getUid());
		bagItemBuilder.setNumber(1);
		// build equip extension of item
		BagEquipmentBase.Builder bagEquipmentBaseBuilder = BagEquipmentBase.newBuilder();
		NatureAttr.Builder natureAttrBuilder = NatureAttr.newBuilder();
		natureAttrBuilder.setAttack(unitWeapon.getAttack());
		natureAttrBuilder.setHitPer(unitWeapon.getHitPer());
		natureAttrBuilder.setCriPer(unitWeapon.getCriPer());
		natureAttrBuilder.setCriDamagePlus(unitWeapon.getCriDamagePlus());
		natureAttrBuilder.setBlock(unitWeapon.getBlock());
		bagEquipmentBaseBuilder.setNature(natureAttrBuilder.build());
		bagEquipmentBaseBuilder.setSight(unitWeapon.getVisionRange());
		bagEquipmentBaseBuilder.setEndurance(unitWeapon.getEndurance());
		bagEquipmentBaseBuilder.setMaxEndurance(unitWeapon.getMaxEndurance());
		bagEquipmentBaseBuilder.setIsEquip(unitWeapon.getIsEquip()==0?false:true);
		BagWeapon.Builder bagWeaponBuilder = BagWeapon.newBuilder();
		bagWeaponBuilder.setAttackRange(unitWeapon.getAttackRange());
		bagWeaponBuilder.setAttackPer(unitWeapon.getAttackPer());
		bagWeaponBuilder.setPenetratePer(unitWeapon.getPenetratePer());
		bagWeaponBuilder.setAttackCountMaxPer(unitWeapon.getAttackCountMaxPer());
		bagWeaponBuilder.setAimTime(unitWeapon.getAimTime());
		bagWeaponBuilder.setReloadTime(unitWeapon.getReloadTime());
		bagWeaponBuilder.setShootNum(unitWeapon.getShootNum());
		bagWeaponBuilder.setCapacity(unitWeapon.getCapacity());
		bagEquipmentBaseBuilder.setBagWeapon(bagWeaponBuilder.build());
		bagItemBuilder.setBagEquipmentBase(bagEquipmentBaseBuilder.build());
		return bagItemBuilder.build();
	}
	
	// -------------- 把游戏中使用的背包数据转为客户端需要的BagItem --------------------
	// TODO 可以调用copyxx函数，减少代码量，但因为BagItem包含了其他结构，还是有一些手动操作需要处理
	// 把角色物品表数据转为客户端需要的BagItem结构
	public static BagItem dbBagItem2BagItem(DbBagItem dbBagItem) {
		BagItem.Builder bagItemBuilder = BagItem.newBuilder();
		bagItemBuilder.setItemId(dbBagItem.getItemId());
		bagItemBuilder.setUid(dbBagItem.getUid());
		bagItemBuilder.setNumber(dbBagItem.getNumber());
		bagItemBuilder.setParam(dbBagItem.getParam());
		return bagItemBuilder.build();
	}
	
	// 把角色装备表数据转为客户端需要的BagItem结构
	public static BagItem dbBagEquipment2BagItem(DbBagEquipment dbBagEquipment) {
		BagItem.Builder bagItemBuilder = BagItem.newBuilder();
		bagItemBuilder.setItemId(dbBagEquipment.getItemId());
		bagItemBuilder.setUid(dbBagEquipment.getUid());
		bagItemBuilder.setNumber(1);
		// build equip extension of item
		BagEquipmentBase.Builder bagEquipmentBaseBuilder = BagEquipmentBase.newBuilder();
		NatureAttr.Builder natureAttrBuilder = NatureAttr.newBuilder();
		natureAttrBuilder.setMaxHp(dbBagEquipment.getHp());
		natureAttrBuilder.setArmor(dbBagEquipment.getArmor());
		natureAttrBuilder.setEvade(dbBagEquipment.getEvade());
		bagEquipmentBaseBuilder.setNature(natureAttrBuilder.build());
		bagEquipmentBaseBuilder.setSight(dbBagEquipment.getSight());
		bagEquipmentBaseBuilder.setEndurance(dbBagEquipment.getEndurance());
		bagEquipmentBaseBuilder.setMaxEndurance(dbBagEquipment.getMaxEndurance());
		bagEquipmentBaseBuilder.setIsEquip(dbBagEquipment.getIsEquip()==0?false:true);
		bagItemBuilder.setBagEquipmentBase(bagEquipmentBaseBuilder.build());
		return bagItemBuilder.build();
	}
	
	// 把角色武器表数据转为客户端需要的BagItem结构
	public static BagItem dbBagWeapon2BagItem(DbBagWeapon dbBagWeapon) {
		BagItem.Builder bagItemBuilder = BagItem.newBuilder();
		bagItemBuilder.setItemId(dbBagWeapon.getItemId());
		bagItemBuilder.setUid(dbBagWeapon.getUid());
		bagItemBuilder.setNumber(1);
		// build equip extension of item
		BagEquipmentBase.Builder bagEquipmentBaseBuilder = BagEquipmentBase.newBuilder();
		NatureAttr.Builder natureAttrBuilder = NatureAttr.newBuilder();
		natureAttrBuilder.setAttack(dbBagWeapon.getAttack());
		natureAttrBuilder.setHitPer(dbBagWeapon.getHitPer());
		natureAttrBuilder.setCriPer(dbBagWeapon.getCriPer());
		natureAttrBuilder.setCriDamagePlus(dbBagWeapon.getCriDamagePlus());
		natureAttrBuilder.setBlock(dbBagWeapon.getBlock());
		bagEquipmentBaseBuilder.setNature(natureAttrBuilder.build());
		bagEquipmentBaseBuilder.setSight(dbBagWeapon.getVisionRange());
		bagEquipmentBaseBuilder.setEndurance(dbBagWeapon.getEndurance());
		bagEquipmentBaseBuilder.setMaxEndurance(dbBagWeapon.getMaxEndurance());
		bagEquipmentBaseBuilder.setIsEquip(dbBagWeapon.getIsEquip()==0?false:true);
		BagWeapon.Builder bagWeaponBuilder = BagWeapon.newBuilder();
		bagWeaponBuilder.setAttackRange(dbBagWeapon.getAttackRange());
		bagWeaponBuilder.setAttackPer(dbBagWeapon.getAttackPer());
		bagWeaponBuilder.setPenetratePer(dbBagWeapon.getPenetratePer());
		bagWeaponBuilder.setAttackCountMaxPer(dbBagWeapon.getAttackCountMaxPer());
		bagWeaponBuilder.setAimTime(dbBagWeapon.getAimTime());
		bagWeaponBuilder.setReloadTime(dbBagWeapon.getReloadTime());
		bagWeaponBuilder.setShootNum(dbBagWeapon.getShootNum());
		bagWeaponBuilder.setCapacity(dbBagWeapon.getCapacity());
		bagEquipmentBaseBuilder.setBagWeapon(bagWeaponBuilder.build());
		bagItemBuilder.setBagEquipmentBase(bagEquipmentBaseBuilder.build());
		return bagItemBuilder.build();
	}
	
	// -------------- 把游戏中使用的背包数据转为通用格式DBagItemPb，以后将用此结构代替BagItem --------------------
	// -------------- 该结构目前用于和dbserver的通信 --------------------
	public static DBagItemPb transUnitBagItem2DBagItemPb(UnitBagItem unitBagItem) {
		if (unitBagItem instanceof UnitWeapon) {
			UnitWeapon weapon = (UnitWeapon)unitBagItem;
			DBagItemPb.Builder builder = DBagItemPb.newBuilder();
			Utils.copySuperFieldsToBean(weapon, builder);
			return builder.build();
		}
		if (unitBagItem instanceof UnitEquipment) {
			UnitEquipment equipment = (UnitEquipment)unitBagItem;
			DBagItemPb.Builder builder = DBagItemPb.newBuilder();
			Utils.copySuperFieldsToBean(equipment, builder);
			return builder.build();
		}
		if (unitBagItem instanceof UnitBagItem) {
			DBagItemPb.Builder builder = DBagItemPb.newBuilder();
			Utils.copySuperFieldsToBean(unitBagItem, builder);
			return builder.build();
		}
		return null;
	}
	
	// -------------- 把游戏中背包变更信息转为通用格式DBagItemPb，方便dbserver做角色背包的数据库操作 --------------------
	public static BagItemChangePb transUnitBagItemChange2BagItemChangePb(UnitBagItemChange unitBagItemChange) {
		UnitBagItem unitBagItem = unitBagItemChange.getItem();
		UnitBagItemOpType opType = unitBagItemChange.getOpType();
		BagItemBean bagItemBean = BagBeanManager.getInstance().getBagItemBeanById(unitBagItem.getItemId());
		if (unitBagItem instanceof UnitWeapon) {
			UnitWeapon weapon = (UnitWeapon)unitBagItem;
			DBagItemPb.Builder builder = DBagItemPb.newBuilder();
			Utils.copySuperFieldsToBean(weapon, builder);
			
			BagItemChangePb.Builder changeBuilder = BagItemChangePb.newBuilder();
			changeBuilder.setBagItem(builder.build());
			changeBuilder.setItemType(bagItemBean.getItemType());
			changeBuilder.setDbType(DBagItemPbOpTypePb.valueOf(opType.value()));
			return changeBuilder.build();
		}
		if (unitBagItem instanceof UnitEquipment) {
			UnitEquipment equipment = (UnitEquipment)unitBagItem;
			DBagItemPb.Builder builder = DBagItemPb.newBuilder();
			Utils.copySuperFieldsToBean(equipment, builder);
			
			BagItemChangePb.Builder changeBuilder = BagItemChangePb.newBuilder();
			changeBuilder.setBagItem(builder.build());
			changeBuilder.setItemType(bagItemBean.getItemType());
			changeBuilder.setDbType(DBagItemPbOpTypePb.valueOf(opType.value()));
			return changeBuilder.build();
		}
		if (unitBagItem instanceof UnitBagItem) {
			DBagItemPb.Builder builder = DBagItemPb.newBuilder();
			Utils.copySuperFieldsToBean(unitBagItem, builder);
			
			BagItemChangePb.Builder changeBuilder = BagItemChangePb.newBuilder();
			changeBuilder.setBagItem(builder.build());
			changeBuilder.setItemType(bagItemBean.getItemType());
			changeBuilder.setDbType(DBagItemPbOpTypePb.valueOf(opType.value()));
			return changeBuilder.build();
		}
		return null;
	}
	
	public static List<BagItemChangePb> transUnitBagItemChange2BagItemChangePb(List<UnitBagItemChange> unitBagItemChangeList)
	{
		List<BagItemChangePb> list = new LinkedList<BagItemChangePb>();
		for (UnitBagItemChange unitBagItemChange : unitBagItemChangeList) {
			list.add(transUnitBagItemChange2BagItemChangePb(unitBagItemChange));
		}
		return list;
	}
	
	// -------------- 把通用格式DBagItemPb转为客户端需要的BagItem结构 --------------------
	public static BagItem transDBagItemPb2BagItem(DBagItemPb bagItemPb) {
		BagItem.Builder bagItemBuilder = BagItem.newBuilder();
		int itemId = bagItemPb.getItemId();
		BagItemBean bagItemBean = BagBeanManager.getInstance().getBagItemBeanById(itemId);
		int itemType = bagItemBean.getItemType();
		if (ItemTypeUtil.isItem(itemType)) {
			Utils.copyFieldsToBean(bagItemPb, bagItemBuilder);
		}
		else if (ItemTypeUtil.isEquipment(itemType)) {
			Utils.copyFieldsToBean(bagItemPb, bagItemBuilder);
			BagEquipmentBase.Builder bagEquipmentBaseBuilder = BagEquipmentBase.newBuilder();
			NatureAttr.Builder natureAttrBuilder = NatureAttr.newBuilder();
			Utils.copyFieldsToBean(bagItemPb, natureAttrBuilder);
			Utils.copyFieldsToBean(bagItemPb, bagEquipmentBaseBuilder);
			bagEquipmentBaseBuilder.setIsEquip(bagItemPb.getIsEquip()==0?false:true);
			bagEquipmentBaseBuilder.setNature(natureAttrBuilder.build());
			bagItemBuilder.setBagEquipmentBase(bagEquipmentBaseBuilder.build());
			
		}
		else if (ItemTypeUtil.isWeapon(itemType)) {
			Utils.copyFieldsToBean(bagItemPb, bagItemBuilder);
			BagEquipmentBase.Builder bagEquipmentBaseBuilder = BagEquipmentBase.newBuilder();
			NatureAttr.Builder natureAttrBuilder = NatureAttr.newBuilder();
			BagWeapon.Builder bagWeaponBuilder = BagWeapon.newBuilder();
			Utils.copyFieldsToBean(bagItemPb, natureAttrBuilder);
			Utils.copyFieldsToBean(bagItemPb, bagEquipmentBaseBuilder);
			Utils.copyFieldsToBean(bagItemPb, bagWeaponBuilder);
			bagEquipmentBaseBuilder.setIsEquip(bagItemPb.getIsEquip()==0?false:true);
			bagEquipmentBaseBuilder.setNature(natureAttrBuilder.build());
			bagEquipmentBaseBuilder.setBagWeapon(bagWeaponBuilder.build());
			bagItemBuilder.setBagEquipmentBase(bagEquipmentBaseBuilder.build());
		}
		else if (ItemTypeUtil.isCollection(itemType)) {
			Utils.copyFieldsToBean(bagItemPb, bagItemBuilder);
		}
		else {
			return null;
		}
		return bagItemBuilder.build();
	}
	
	public static List<BagItem> transDBagItemPb2BagItem(List<DBagItemPb> list) {
		List<BagItem> bagItems = new LinkedList<BagItem>();
		for (DBagItemPb bagItemPb : list) {
			bagItems.add(transDBagItemPb2BagItem(bagItemPb));
		}
		return bagItems;
	}
}
